package com.xxpt.common;

import com.xxpt.common.constant.SysConstant;
import com.xxpt.common.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 启动监听
 * @Author m.sh.lin0328@163.com - 2019/5/4 11:52
 */
@Slf4j
public class BaseRunnerListener implements ApplicationRunner {

    @Autowired
    ApplicationContext context;

    @Autowired
    Environment env;

    @Autowired
    RedisTemplate redisTemplate;

    //@Autowired
    //RabbitMqUtil rabbitMqUtil;

    @Value("${server.port}")
    Integer port;

    //@Value("${server.servlet.context-path}")
    //String serverPath;

    /*@InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }*/

    @Override
    public void run(ApplicationArguments args) throws Exception {
        SysConstant.CURR_PROFILE = env.getActiveProfiles()[0];

        log.info("\r\n★★★★★☆☆★★★★★--系统启动成功.... --★★★★★☆☆★★★★★\r\n");
        log.info("-------------当前启动环境为：" + SysConstant.CURR_PROFILE + "----------");

        SpringContextUtil.setApplicationContext(context);
        SysConstant.redisTemplate = redisTemplate;

        //TimeZone.setDefault(TimeZone.getTimeZone("GTM+8"));
        //发送 mq 消息
        //rabbitMqUtil.sendMessage(Queues.TES01_QUEUE,"{\"name\":\"小晴\",\"age\":20}");

        initialize();
    }

    /**
     * 初始化任务
     */
    public void initialize() {
        //打开启动页
        openStartPage();
        //定时更新参数
        //loadSysParamsThread();

        addCache();
    }

    /**
     * 写入缓存
     */
    public void addCache(){

    }

    /**
     * 打开启动页面
     */
    public void openStartPage() {
        /*try {
            String url = "http://localhost:" + port + "/" + serverPath;
            if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
                //Desktop.getDesktop().browse(new URI(url));
                Runtime.getRuntime().exec(
                        "cmd   /c   start  " + url);
            } else {
                Runtime.getRuntime().exec("xdg-open " + url);
            }
        } catch (Exception ex) {
        }*/
    }
}
